from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.views import View
from django.views.generic.base import TemplateView            # 导入模板视图
from commodity.models import *
from django.core.cache import cache
# Create your views here.

# 前后端不分离TemplateView 模板属土类
class IndexView_template(TemplateView):
    template_name = 'index.html'
    extra_context = {
        'title':'首页',
    }

    def get_context_data(self, **kwargs):
        # 创建上下文字典
        context = super(IndexView_template, self).get_context_data(**kwargs)
        # 今日必抢，获取销量最高的前10个产品
        context['sold_max'] = sold_max = (Product.objects.all().order_by('-sold')[:10])
        # # 电子产品销量前5
        context['dianz_5'] = Product.objects.filter(
            types__first_id__first='电子产品'
        ).select_related('types', 'types__first_id').order_by('-sold')[:5]
        # 超市零售销量前5
        context['ls_5'] = Product.objects.filter(
            types__first_id__first='超市零售'
        ).select_related('types', 'types__first_id').order_by('-sold')[:5]
        # 母婴产产品销量前五
        context['mu_5'] = Product.objects.filter(
            types__first_id__first='母婴用品'
        ).select_related('types', 'types__first_id').order_by('-sold')[:5]

        print(context['dianz_5'])
        return context


class IndexView(View):
    # 数据序列化
    def product_to_dict(self,product):
        return {
            'id':product.id,
            'name':product.name,
            'price':str(product.price),
            'sold':str(product.sold),
            'likes':str(product.likes),
            'img':str(product.img),
            'details':str(product.details),
            'describe_str':str(product.describe_str),
        }

    def get(self, request):
        return render(request,'index.html')
        # return HttpResponse('Hello, world. You\'re at the polls index.')

    def post(self, request):
        # 今日必抢，获取销量最高的前10个产品
        # 增加缓存
        sold_max = cache.get('sold_max')
        if not sold_max:
            sold_max = (Product.objects.all().order_by('-sold')[:10])
            sold_max = [self.product_to_dict(i) for i in sold_max]
            cache.set('sold_max', sold_max, 3600)

        # 电子产品销量前5
        dianz_5 = Product.objects.filter(
            types__first_id__first='电子产品'
        ).select_related('types', 'types__first_id').order_by('-sold')[:5]

        # 超市零售销量前5
        ls_5 = Product.objects.filter(
            types__first_id__first='超市零售'
        ).select_related('types', 'types__first_id').order_by('-sold')[:5]

        # 母婴产产品销量前五
        mu_5 = Product.objects.filter(
            types__first_id__first='母婴用品'
        ).select_related('types', 'types__first_id').order_by('-sold')[:5]

        # 数据序列化
        dianz_5 = [self.product_to_dict(i) for i in dianz_5]
        ls_5 = [self.product_to_dict(i) for i in ls_5]
        mu_5 = [self.product_to_dict(i) for i in mu_5]

        # 转为json数据
        json_data = {
            'sold_max': sold_max,
            'ls_5': ls_5,
            'mu_5': mu_5,
            'dianz_5': dianz_5,
        }

        return JsonResponse(json_data)
